﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BTN04.BUS;
using BTN04.DTO;
using System.Collections.Generic;

namespace BTN04.USER
{
    public partial class timkiemnangcao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGiaTuGiaDen();
            LoadLoaiSanPham();
            LoadXuatSu();
            LoadNhaSanXuat();
            if (Request.QueryString["MaNhaSanXuat"] == null)
            {

                Repeater1.Visible = false;
                Label1.Visible = false;
            }
            else
            {
                Repeater1.Visible = true;
                Label1.Visible = false;
                String TenSanPham = Request.QueryString["TenSP"];
                int MaNhaSanXuat;
                int MaXuatSu;
                int MaLoaiSanPham;
                int GiaTu;
                int GiaDen;
                try
                {
                    MaNhaSanXuat = Convert.ToInt32(Request.QueryString["MaNhaSanXuat"]);
                }
                catch
                {
                    MaNhaSanXuat = 0;
                }
                try
                {
                    MaXuatSu = Convert.ToInt32(Request.QueryString["MaXuatSu"]);
                }
                catch
                {
                    MaXuatSu = 0;
                }
                try
                {
                    MaLoaiSanPham = Convert.ToInt32(Request.QueryString["MaLoaiSanPham"]);
                }
                catch
                {
                    MaLoaiSanPham = 0;
                }
                try
                {
                    GiaTu = Convert.ToInt32(Request.QueryString["GiaTu"]);
                }
                catch
                {
                    GiaTu = 0;
                }
                try
                {
                    GiaDen = Convert.ToInt32(Request.QueryString["GiaDen"]);
                }
                catch
                {
                    GiaDen = 0;
                }
                txtTenSanPham.Text = TenSanPham;

                SanPhamBUS spBUS = new SanPhamBUS();
                DataTable dt = new DataTable();
                dt = spBUS.TimKiemNangCao(TenSanPham, GiaTu, GiaDen, MaNhaSanXuat, MaLoaiSanPham, MaXuatSu,1,12);
                if (dt.Rows.Count == 0)
                {
                    Repeater1.Visible = false;
                    Label1.Visible = true;
                    Label1.Text = "Không tìm thấy kết quả";
                }

            }
        }
        public void LoadNhaSanXuat()
        {
            List<NhaSanXuatDTO> lstNhaSanXuat = new List<NhaSanXuatDTO>();
            NhaSanXuatBUS nsxBUS = new NhaSanXuatBUS();
            lstNhaSanXuat = nsxBUS.DanhSach();
            NhaSanXuatDTO nsxDTO = new NhaSanXuatDTO();
            nsxDTO.MaNhaSanXuat = 0;
            nsxDTO.TenNhaSanXuat = "--Chon Nha San Xuat--";
            ListItem item;
            lstNhaSanXuat.Insert(0, nsxDTO);

            for (int i = 0; i < lstNhaSanXuat.Count; i++)
            {
                item = new ListItem();
                item.Text = lstNhaSanXuat[i].TenNhaSanXuat;
                item.Value = lstNhaSanXuat[i].MaNhaSanXuat.ToString();
                ddlNhaSanXuat.Items.Add(item);
            }
        }
        public void LoadXuatSu()
        {
            List<XuatSuDTO> lstXuatSu = new List<XuatSuDTO>();
            XuatSuBUS xsBUS = new XuatSuBUS();
            XuatSuDTO xsDTO = new XuatSuDTO();
            lstXuatSu = xsBUS.DanhSach();
            xsDTO.MaXuatSu = 0;
            xsDTO.TenXuatSu = "--Chon Xuat Su --";
            lstXuatSu.Insert(0, xsDTO);
            ListItem item;
            for (int i = 0; i < lstXuatSu.Count; i++)
            {
                item = new ListItem();
                item.Text = lstXuatSu[i].TenXuatSu;
                item.Value = lstXuatSu[i].MaXuatSu.ToString();
                ddlXuatSu.Items.Add(item);
            }
        }
        public void LoadLoaiSanPham()
        {
            List<LoaiSanPhamDTO> lstLoaSanPham = new List<LoaiSanPhamDTO>();
            LoaiSanPhamBUS lspBUS = new LoaiSanPhamBUS();
            lstLoaSanPham = lspBUS.DanhSachLoaiSanPham();
            LoaiSanPhamDTO lspDTO = new LoaiSanPhamDTO();
            lspDTO.MaLoaiSanPham = 0;
            lspDTO.TenLoaiSanPham = "--Chon Loai San Pham--";
            lstLoaSanPham.Insert(0, lspDTO);
            ListItem item;
            for (int i = 0; i < lstLoaSanPham.Count; i++)
            {
                item = new ListItem();
                item.Text = lstLoaSanPham[i].TenLoaiSanPham;
                item.Value = lstLoaSanPham[i].MaLoaiSanPham.ToString();
                ddlLoaiSanPham.Items.Add(item);
            }
        }
        public void LoadGiaTuGiaDen()
        {
            //gia tu
            ListItem item;
            item = new ListItem();
            item.Value = "0";
            item.Text = "--Chon Gia Tu--";
            ddlGiaTu.Items.Add(item);
            item = new ListItem();
            item.Value = "1";
            item.Text = "500000";
            ddlGiaTu.Items.Add(item);
            item = new ListItem();
            item.Value = "2";
            item.Text = "1000000";
            ddlGiaTu.Items.Add(item);
            item = new ListItem();
            item.Value = "3";
            item.Text = "2000000";
            ddlGiaTu.Items.Add(item);
            item = new ListItem();
            item.Value = "4";
            item.Text = "3000000";
            ddlGiaTu.Items.Add(item);
            item = new ListItem();
            item.Value = "5";
            item.Text = "5000000";
            ddlGiaTu.Items.Add(item);
            //gia den
            item = new ListItem();
            item.Value = "0";
            item.Text = "--Chon Gia Den--";
            ddlGiaDen.Items.Add(item);
            item = new ListItem();
            item.Value = "1";
            item.Text = "1000000";
            ddlGiaDen.Items.Add(item);
            item = new ListItem();
            item.Value = "2";
            item.Text = "2000000";
            ddlGiaDen.Items.Add(item);
            item = new ListItem();
            item.Value = "3";
            item.Text = "3000000";
            ddlGiaDen.Items.Add(item);
            item = new ListItem();
            item.Value = "4";
            item.Text = "5000000";
            ddlGiaDen.Items.Add(item);
            item = new ListItem();
            item.Value = "5";
            item.Text = "10000000";
            ddlGiaDen.Items.Add(item);
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            int indexGiaTu = ddlGiaTu.SelectedIndex;
            int indexGiaDen = ddlGiaDen.SelectedIndex;
            int indexLoaiSanPham = ddlLoaiSanPham.SelectedIndex;
            int indexXuatSu = ddlXuatSu.SelectedIndex;
            int indexNhaSanXuat = ddlNhaSanXuat.SelectedIndex;

            int _giaTu;
            int _giaDen;
            int _maLoaiSanPham;
            int _maXuatSu;
            int _maNhaSanXuat;
            if (indexGiaTu == 0)
            {
                _giaTu = 0;
            }
            else
                _giaTu = Convert.ToInt32(ddlGiaTu.Items[indexGiaTu].Text);


            if (indexGiaDen == 0)
            {
                _giaDen = 0;
            }
            else
                _giaDen = Convert.ToInt32(ddlGiaDen.Items[indexGiaDen].Text);
            _maLoaiSanPham = Convert.ToInt32(ddlLoaiSanPham.Items[indexLoaiSanPham].Value);
            _maNhaSanXuat = Convert.ToInt32(ddlNhaSanXuat.Items[indexNhaSanXuat].Value);
            _maXuatSu = Convert.ToInt32(ddlXuatSu.Items[indexXuatSu].Value);
            Response.Redirect("~/GUEST/timkiemnangcao.aspx?TenSP=" + txtTenSanPham.Text + "&MaXuatSu=" + _maXuatSu + "&MaLoaiSanPham=" + _maLoaiSanPham + "&MaNhaSanXuat=" + _maNhaSanXuat + "&GiaTu=" + _giaTu + "&GiaDen=" + _giaDen);
        }
    }
}
